
import * as type from './type';
import * as http from '../axios/index';

const requestData = category => ({
    type: type.REQUEST_DATA,
    category
});
export const receiveData = (data, category) => ({
    type: type.RECEIVE_DATA,
    data,
    category
});
/**
 * 请求数据调用方法
 * @param method  默认'POST'
 * @param action  请求接口的函数名
 * @param params  请求接口的参数
 */
export const fetchData = ({method='POST', action, params={}, stateName}) => dispatch => {
    !stateName && (stateName = action);
    console.log(`[请求接口]:${action} \n[参数]:${params}`);
    dispatch(requestData(stateName));
    if (method === 'POST') {
        return http.api_post(action, params)
            .then(res => dispatch(receiveData(res, stateName)));
    }
};